﻿/*
 *	Copyright (c) 2009 Queensland University of Technology. All rights reserved.
 *	The QUT Bioinformatics Collection is open source software released under the 
 *	Microsoft Public License (Ms-PL): http://www.microsoft.com/opensource/licenses.mspx.
 */
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace QUT.Bio.Util {
	/// <summary>
	/// A reference to a remote object.
	/// </summary>

	public class Xref: IComparable<Xref>, IEquatable<Xref> {
		public string Database {
			get;
			private set;
		}
		
		public string Id {
			get;
			private set;
		}
		
		public Xref ( string database, string id ) {
			Database = database;
			Id = id;
		}

		public override string ToString () {
			return String.Format( "{0}:{1}", Database, Id );
		}
		
		/// <summary>
		/// Gets a composite key consisting of the database and Id.
		/// </summary>
		
		public string Key {
			get {
				return string.Format( "{0}|{1}", Database, Id );
			}
		}

		/// <summary>
		/// Partial order Xref objects, ranks by Id within Database.
		/// </summary>
		/// <param name="other"></param>
		/// <returns>Less than 0 if this ranks ahead of the other; Greater than 0 if this ranks after the other; 0 if they are equivalent.</returns>

		public int CompareTo ( Xref other ) {
			int result = Database.CompareTo( other.Database );
			
			if ( result == 0 ) {
				result = Id.CompareTo( other.Id );
			}
			
			return result;
		}

		/// <summary>
		/// Returns true iff this Xref has the same database and Id as the supplied argument.
		/// </summary>
		/// <param name="other"></param>
		/// <returns>true iff this Xref has the same database and Id as the supplied argument.</returns>

		public bool Equals ( Xref other ) {
			return CompareTo( other ) == 0;
		}

		/// <summary>
		/// Returns true iff this Xref has the same database and Id as the supplied argument.
		/// </summary>
		/// <param name="obj">An object against which the XRef is to be compared.</param>
		/// <returns>true iff this Xref has the same database and Id as the supplied argument.</returns>
		/// <exception cref="InvalidCastException">InvalidCastException if other is not a Xref.</exception>

		public override bool Equals ( object obj ) {
			return obj is Xref ? Equals( (Xref) obj ) : false;
		}
		
		/// <summary>
		/// Gets a hash code for this object that is consistent with Equals.
		/// </summary>
		/// <returns></returns>

		public override int GetHashCode () {
			return Database.GetHashCode() << 5 + Id.GetHashCode();
		}
	}
}
